<?php
include("../db_connect.php");

?>
<script>
$(document).ready(function (){
$("#traderdata").html(loader).load("admin/traderdata.php");


$("#traderstatus").change(function () {
var status = $(this).val();

	$("#traderdata").html(loader).load("admin/traderdata.php?status="+status);

});

$("#tradeaccept").click(function(){

	var tradearray = new Array();
	$("#traderdata tr td").find("input:checked").each(function(e){
			tradearray[e] = $(this).val();
		});
	
		if (tradearray.length <=0){
				$("#msgbox").empty().html("no row selected");
				$("#msgbox").dialog({
					modal:true,
					title:"Error",
					width:350,
					height: 200,
					show: "fade",
					hide: "fade",
					buttons: {
						OK: function() {
							$( this ).dialog( "close" );
						}

					},
				});
				
				

		}
		else if(tradearray.length >=1){
		var disap = "approve";
		$("#msgbox").empty().html("Trade?");
			$("#msgbox").dialog({
				modal:true,
				width:350,
				height: 200,
				show:"fade",
				hide: "fade",
				buttons: {
					OK: function() {
						$( this ).dialog( "close" );
						approveRequest(tradearray, disap);
					},
					CANCEL: function() {
						$( this ).dialog( "close" );
					}
				},

			});
			

		}

});//end of approve.click()

$("#tradedeny").click(function(){

	var tradearray = new Array();
	$("#traderdata tr td").find("input:checked").each(function(e){
			tradearray[e] = $(this).val();
		});
	
		if (tradearray.length <=0){
				$("#msgbox").empty().html("no row selected");
				$("#msgbox").dialog({
					modal:true,
					title:"Error",
					width:350,
					height: 200,
					show: "fade",
					hide: "fade",
					buttons: {
						OK: function() {
							$( this ).dialog( "close" );
						}

					},
				});
				
				

		}
		else if(tradearray.length >=1){
		var disap = "disaprove";
		$("#msgbox").empty().html("Deny this trade?");
			$("#msgbox").dialog({
				modal:true,
				width:350,
				height: 200,
				show:"fade",
				hide: "fade",
				buttons: {
					OK: function() {
						$( this ).dialog( "close" );
						approveRequest(tradearray, disap);
					},
					CANCEL: function() {
						$( this ).dialog( "close" );
					}
				},

			});
			

		}

});//end of approve.click()

$("#tradedelete").click(function(){

	var tradearray = new Array();
	$("#traderdata tr td").find("input:checked").each(function(e){
			tradearray[e] = $(this).val();
		});
	
		if (tradearray.length <=0){
				$("#msgbox").empty().html("no row selected");
				$("#msgbox").dialog({
					modal:true,
					title:"Error",
					width:350,
					height: 200,
					show: "fade",
					hide: "fade",
					buttons: {
						OK: function() {
							$( this ).dialog( "close" );
						}

					},
				});
				
				

		}
		else if(tradearray.length >=1){
		var disap = "approve";
		$("#msgbox").empty().html("Trade?");
			$("#msgbox").dialog({
				modal:true,
				width:350,
				height: 200,
				show:"fade",
				hide: "fade",
				buttons: {
					OK: function() {
						$( this ).dialog( "close" );
						tradedelete(tradearray);
					},
					CANCEL: function() {
						$( this ).dialog( "close" );
					}
				},

			});
			

		}

});//

function tradedelete(tradearray){
		var type = "delete";
		$.ajax({
			url:"admin/traderdelete.php?id="+tradearray,
			success:function(data){
				$("#msgbox").html("You Successfully Delete a Trade");
				$("#msgbox").dialog({
					modal:true,
					title:"",
					width:350,
					height: 200,
					show: "fade",
					hide: "fade",
					buttons: {
						OK: function() {
							$( this ).dialog( "close" );
							location.reload();
								
						}

					},
				});
			}
		});
			
		
}


/*approve request user*/
 function approveRequest(tradearray,app){
	
	if(app =="approve"){
		$.ajax({
			url:"admin/tradeverifyrequest.php?itemRequest="+tradearray+"&approve="+app,
			success:function(data){
			
			if(data){
					//$("msgbox").empty().html("");
					$("#msgbox").html("You Successfully made a trade");
						$("#msgbox").dialog({
							modal:true,
							title:"",
							width:350,
							height: 200,
							show: "fade",
							hide: "fade",
							buttons: {
								OK: function() {
									$( this ).dialog( "close" );
									location.reload();
									
								}

							},
						});
				}
				else{
					$("#msgbox").empty().html("something went wrong");
						$("#msgbox").dialog({
							modal:true,
							title:"Error",
							width:350,
							height: 200,
							show: "fade",
							hide: "fade",
							buttons: {
								OK: function() {
									$(this).dialog( "close" );
								}

							},
						});
				
				}
			
			
			}


		});

	}else{
	
	$.ajax({
			url:"admin/tradeverifyrequest.php?itemRequest="+tradearray+"&approve="+app,
			success:function(data){
			
			
				if(data){
					//$("msgbox").empty().html("");
					$("#msgbox").html("Sucessfully disapproved");
						$("#msgbox").dialog({
							modal:true,
							title:"",
							width:350,
							height: 200,
							show: "fade",
							hide: "fade",
							buttons: {
								OK: function() {
									$( this).dialog( "close" );
									location.reload();
								}

							},
						});
				}
				else{
					$("msgbox").empty().html("something went wrong");
						$("#msgbox").dialog({
							modal:true,
							title:"Error",
							width:350,
							height: 200,
							show: "fade",
							hide: "fade",
							buttons: {
								OK: function() {
									$( this).dialog( "close" );
								}

							},
						});
				
				}
			
			
			}


		});
	
	
	}

}


});//end of document



function delete_item(value){
	if(confirm("Do you want to delete this?")){
	$.ajax({
		
		url:"admin/modifyData.php?category=auto&action=delete&pid="+value,
		success:function(data){
			if(data){
			alert("Successful Delete");
			//$("#itemRequest").html(loader).load("admin/itemRequest.php?brand="+brand+"&model="+model+"&fname="+fname+"&lname="+lname);
			$("#itemRequest").html(loader).load("admin/itemRequest.php");
			}
			else{
				alert("Unsuccessful Delete");
			}
			
		}
	});
	}

}
checked = false;
function checkalltrade(){
	if (checked == false){
		checked = true
	}
	else{
	checked = false
	}
	for (var i = 0; i < document.getElementById('mytraderequest').elements.length; i++){
		document.getElementById('mytraderequest').elements[i].checked = checked;
	}	
}


</script>

<div id="searchbox">
<!--search div-->
<div>
	<div class="headersearch ">
		<label>Action:</label><button id="tradeaccept" name="display">Accept Trade</button> 
		<button id="tradedeny" name="out">Deny Trade</button>
		<button id="tradedelete" name="out">Delete</button>
		<label>Status:</label>
		<select id="traderstatus">
			<option value="">All</option>
			<option value="Confirm">Confirm</option>
			<option value="Decline">Decline</option>
			<option value="Pending">Pending</option>
		</select>
		
	</div>
</div>			
		
<!--end search div-->		
</div>

<div id="traderdata" class="itemRequest2" style="overflow:auto">
	<?php //include("itemRequest.php"); ?>
</div>